home *** CD-ROM | disk | FTP | other *** search
/ Scene 96 / Scene 96 International Edition (Zyklop Software) (Disc 2) (1997).iso / graphics / artpacks / acid0896 / xbin.txt < prev   
Text File  |  1996-07-31  |  21KB  |  471 lines

  1.  
  2.  
  3.                        eXtended BIN Format specification
  4.                       -----------------------------------
  5.  
  6.                    Written by Tasmanic Tasmaniac / ACiD 1996
  7.                      No rights reserved, No patent pending
  8.                      For comments/questions : Tas@acid.org
  9.                    http://uc2.unicall.be/tasmaniac/xbin.htm
  10.  
  11.  
  12.   Introduction
  13.   ------------
  14.  
  15.   Out of a crying need from several ANSi artists, a new type of file is
  16.   born and ready to take the Art scene by storm.
  17.  
  18.   This new format is known as eXtended BIN or XBIN for short.
  19.  
  20.   XBIN is what it's name dictates, it's an extention to the normal raw-image
  21.   BIN files which have become very popular among the art-scene lately.
  22.  
  23.   The use of the XBIN format is more or less the same as for the BIN format.
  24.   However, XBIN offers a far better way to handle the the raw images.
  25.  
  26.  
  27.   BIN vs XBIN
  28.   ------------
  29.  
  30.   The BIN format was introduced into the art-scene out of a need to overcome
  31.   the limits of ANSi files.  Apparantly, the 80 columns wide screen was too
  32.   constraining for some artists.  The BIN format was adopted to resolve this
  33.   problem.
  34.  
  35.   Being very simple in nature, BIN was quickly supported by several art
  36.   groups in their native ANSI/RIP/GIF/etc. viewer.  Consequently, our very
  37.   own SAUCE standard went in for a quick facelift and immediately dealt with
  38.   one of the main problems imposed by the BIN format.
  39.  
  40.   Being nothing more than a raw memory copy of the textmode videomemory, BIN
  41.   offers no insight to the size/width of the image.  Having nothing more than
  42.   a BIN file, there is no way to determine whether it's a 80 column wide or
  43.   a 160 column wide image.  SAUCE took care of this by taking the BIN format
  44.   into it's specifications.  Out of the SAUCE attached to the BIN, one was
  45.   able to determine the correct dimensions of the BIN.
  46.  
  47.   XBIN solves this little matter all by itself, and takes matters even
  48.   further.  Anything BIN can do, XBIN does better.
  49.  
  50.  
  51.   Features
  52.   ---------
  53.  
  54.   XBIN allows for binary images up to 65536 columns wide, and 65536 lines
  55.   high.
  56.  
  57.   XBIN allows for an alternate set of palette colors either in blink or
  58.   non-blink mode.
  59.  
  60.   XBIN allows for different textmode fonts from 1 to 32 scanlines high,
  61.   consisting of either 256 or 512 different characters.
  62.  
  63.   And finally, XBIN offers smaller files due to a simple yet very efficient
  64.   compression system.
  65.  
  66.  
  67.   Goal
  68.   -----
  69.  
  70.   XBIN was designed with certain rules in mind.  These were:
  71.  
  72.   - The format may not have a group-specific name.  It should be as
  73.     anonymous as possible.
  74.  
  75.   - The format must be simple to implement, yet offer as much functionality
  76.     as possible.
  77.  
  78.   - The format must be universally adoptable, a prerequisite for this is the
  79.     complete availability of the formats specifications.
  80.  
  81.   - Availability of direct plug-in code, tools to read/write/convert the
  82.     format and the immediate support in practical applications.
  83.  
  84.   The above simple four rules were similarly used when designing the
  85.   specifications of SAUCE.  These basic principles helped get the acceptance
  86.   of SAUCE as a standard by the majority of art groups worldwide.  The
  87.   premise of SAUCE's success was reason alone to re-use this set of rules
  88.   while designing XBIN.
  89.  
  90.   More specifically to the practical use of XBIN, following constraints and
  91.   ideas needed to be addressed :
  92.  
  93.   - Provide an unambiguous system for how the file should be displayed.
  94.  
  95.   - Be as complete as possible to avoid having to make changes to the
  96.     specifications.  Since XBIN has a very specific use (textmode 'graphics')
  97.     it should not be too difficult to provide for all/most of what textmode
  98.     has to offer.
  99.     In short XBIN should provide for any possible feature possible for
  100.     textmode graphics, but no more.
  101.     In addition, XBIN is designed for VGA.  Several of the features XBIN
  102.     offers can only be used on a VGA system.
  103.  
  104.     To sum things up, XBIN was required to handle :
  105.  
  106.     - Any reasonable sized image
  107.     - Alternate color sets (palettes)
  108.     - Alternate fonts of any size  (due to limitations of the VGA hardware
  109.       this means sizes of 1 up to 32 pixels), for practical reasons, only 8
  110.       pixel wide fonts are supported, even though the VGA hardware has other
  111.       capabilities.
  112.     - Blink and non-blink mode
  113.     - 256 and 512 characters
  114.  
  115.   In addition to the set requirements, XBIN also offers a simple compression
  116.   scheme which does remarkably well seen it's simplicity.
  117.  
  118.  
  119.   On to the Specs...
  120.   -------------------
  121.  
  122.   Ok, now that we have you totally brainwashed, and drooling, it's time for
  123.   dealing out the specs.
  124.  
  125.   First of all, the default file extention of an XBIN file is '.XB', just as
  126.   a BIN file by default has '.BIN' as extention.  Even though long filenames
  127.   are getting popular due to newer operating systems like Windows 95,
  128.   Windows NT, OS/2, Unix, Linux... there was specifically chosen for .XB
  129.   over .XBIN
  130.  
  131.   An XBIN consists of 4 main parts, a header (required), a palette
  132.   (optional), a font (optional), and the image data (optional).
  133.  
  134.     ┌────────────┐
  135.     │ Header     │ Required
  136.     ├────────────┤
  137.     │ Palette    │ Optional
  138.     ├────────────┤
  139.     │ Font       │ Optional
  140.     ├────────────┤
  141.     │ Image Data │ Optional
  142.     └────────────┘
  143.  
  144.   ! NOTE !
  145.   An XBIN is not REQUIRED to have an image.  You can use the standard XBIN
  146.   format for storing pre-made fonts and pre-made palettes which you could
  147.   load from a viewer/editor (as alternate font/palette)
  148.  
  149.  
  150.     Header
  151.     ------
  152.     The XBIN header consists of 11 bytes.  The header describes the size of
  153.     the image, how the screen should be set up, and how the rest of the
  154.     XBIN should be processed.
  155.  
  156.     Record layout :
  157.  
  158.     Name       Size Type    Purpose
  159.     ---------- ---- ------- -------------------------------------------------
  160.     ID         4    Char    XBIN identification, these 4 bytes should contain
  161.                             the text "XBIN".  Any file which does not have a
  162.                             matching ID should not be considered to be an
  163.                             XBIN file.
  164.     EofChar    1    Char    End of file character (Ctrl-Z, Ascii 26, 1A hex)
  165.                             When a user uses type to view the file, he'll
  166.                             see "XBIN" printed on screen.
  167.     Width      2    Numeric Width of the image in character columns.
  168.     Height     2    Numeric Height of the image in character rows.
  169.     Fontsize   1    Numeric Number of pixel rows (scanlines) in the font,
  170.                             Default value for VGA is 16.
  171.                             Any value from 1 to 32 is technically possible on
  172.                             VGA.  Any other values should be considered
  173.                             illegal.
  174.     Flags      1    Bits    A set of flags indicating special features in the
  175.                             XBIN file.  More on this later.
  176.  
  177.     A sample XBIN header in Pascal could be :
  178.  
  179.           TYPE  XB_Header = RECORD
  180.                               ID      : ARRAY[0..3] OF Char;
  181.                               EofChar : Byte;
  182.                               Width   : Word;
  183.                               Height  : Word;
  184.                               Fontsize: Byte;
  185.                               Flags   : Byte;
  186.                             END;
  187.  
  188.     A sample XBIN header in C could be :
  189.  
  190.           typedef struct XB_Header {
  191.              unsigned char  ID[4];
  192.              unsigned char  EofChar;
  193.              unsigned short Width;
  194.              unsigned short Height;
  195.              unsigned char  Fontsize;
  196.              unsigned char  Flags;
  197.           };
  198.  
  199.  
  200.     Flags
  201.     -----
  202.     The 'flags' field in the XBIN header tells us more on special features
  203.     the XBIN has in use.  The flags field consists out of 8 separate bits
  204.     each with it's unique function.
  205.     Bits which are 0, are considered OFF or disabled, Bits with 1 are
  206.     considered ON or enabled.
  207.  
  208.     Bit                 Name     Purpose
  209.     ------------------- -------- --------------------------------------------
  210.     [ . . . . . . . X ] Palette  When 1, A palette is present in the XBIN.
  211.                                  When 0, Default palette applies.
  212.     [ . . . . . . X . ] Font     When 1, A Font is present in the XBIN.
  213.                                  When 0, Default font applies, since the VGA
  214.                                  default fontsize is 16, This bit should be 1
  215.                                  for other sizes.  In consequence, a font
  216.                                  should then also be present.
  217.     [ . . . . . X . . ] Compress When 1, XBIN compression is used
  218.                                  When 0, No compression is used, the image
  219.                                  is stored in raw memory format.
  220.     [ . . . . X . . . ] NonBlink When 1, Image should be shown in non-blink
  221.                                  mode.
  222.                                  When 0, Image should be shown in blink mode.
  223.     [ . . . X . . . . ] 512Chars When 1, The image is built up out of 512
  224.                                  characters in stead of the usual 256.
  225.                                  This bit also requires the Font bit to be
  226.                                  set since the VGA has no default font for
  227.                                  512 character mode.
  228.                                  The font in consequence then has 512
  229.                                  characters in stead of the usual 256.
  230.     [ X X X . . . . . ] Unused   Unused, should be all zeroes.
  231.  
  232.  
  233.     Palette
  234.     --------
  235.     A palette is only present when the 'Palette' bit is set in the Flags
  236.     field of the XBIN header.
  237.     The palette is built up of 48 bytes, a red, green and blue value (in
  238.     that order) for each of the 16 colors.
  239.     Each palette value can range from 0 to 63.
  240.  
  241.  
  242.     Font
  243.     -----
  244.     A Font is only present when the 'Font' bit is set in the Flags field of
  245.     the XBIN header.
  246.     For each character (256, or 512 when the '512Chars' bit is set in the
  247.     Flags field) FontSize bytes are stored in sequence.  The character set
  248.     is defined from the top row of each character matrix to the bottom row.
  249.  
  250.     In a 16 pixel high font, the first 16 bytes are the fontmatrix for ascii
  251.     value 0, the next 16 are for ascii 1 and so on.  In total, a 16 pixel
  252.     font would have a font of 4096 bytes (16*256)
  253.     Technically, the biggest font possible would be 16Kb in size. (32 pixels
  254.     high, 512 characters), and the smallest would be 256 bytes (1 pixel high,
  255.     256 characters).
  256.  
  257.  
  258.     Image data
  259.     -----------
  260.     The image data is a raw image of video memory.  Each character consists
  261.     of 2 bytes, the first being the character, the second being the attribute
  262.     (color).
  263.     The Image data would thus be equal to Width*Height*2.  The biggest XBIN
  264.     would be a whopping 8Gb (65535*65535*2) in size and the smallest would be
  265.     0 bytes (0*0*2).
  266.  
  267.     Unless the 'Compress' bit is set in the Flags field, Image data is stored
  268.     in the exact way you would need it in video memory.
  269.  
  270.     When the 'Compress' bit is set, Image data is compressed with
  271.     XBIN-Compression.  This is a fairly simple compression system, which
  272.     should pose no real difficulty to decompress.  As compression goes
  273.     however an XBIN compressor is a little harder to write <g>.
  274.  
  275.  
  276.     XBIN Compression
  277.     ----------------
  278.     The XBIN compression uses a slightly improved Run-Length Encoding scheme
  279.     which will do very well on this type of data.
  280.  
  281.     In stead of describing how a compressor would work, I'll explain how the
  282.     compression works by giving some examples.  In these examples, you'll see
  283.     strings of characters more or less like this one :
  284.  
  285.          Aa,Ab,Ac,Ba,Bb,Bc,De,Zx,Yu
  286.  
  287.     This string represents a part of the uncompressed data. The capital
  288.     lettres are character bytes, the lower case letters are attribute bytes.
  289.  
  290.     OK, pay close attention, as things may get hairy now ;-)
  291.  
  292.     When you examine an ANSi or a BIN file, you may or may not have noticed
  293.     several characteristics which are typical for ANSi/BIN files.
  294.     You see sequences of identical characters one after another, and you see
  295.     sequences of identical colors one after another.  You may even see the
  296.     combination of both; identical characters in identical colors one after
  297.     another.
  298.  
  299.     XBIN-compression makes use of these characteristics by replacing
  300.     sequences of identical characters/color with a counter and the actual
  301.     data.
  302.     A sequence like :
  303.  
  304.         Aa,Aa,Aa,Aa,Aa,Aa,Aa,Aa,Aa,Bb,Bb,Bb,Bb
  305.  
  306.     could easily be replaced with
  307.  
  308.         [Repeat 9 times]Aa,[Repeat 4 times]Bb
  309.  
  310.     The '[Repeat x times]' tag is the repeat counter.
  311.  
  312.     If you're a smart observer, you've probably already figured where I'm
  313.     driving at.  It shouldn't be too hard to figure out that there's a need
  314.     for four different types of compression :
  315.       1) No compression  (when two subsequent character/attribute pairs have
  316.                           no relation)
  317.       2) character compression  (for a sequence of identical characters in
  318.                                  different colors)
  319.       3) attribute compression  (for a sequence of different characters with
  320.                                  identical attributes)
  321.       4) character/attribute compression (for a sequence of identical
  322.                                           character/attributes).
  323.  
  324.     Hmm.. 4 types of compression.. That would nicely fit in 2 bits, leaving
  325.     6 bits in a byte unused.  Now.. what if we were to use those 6 bits for
  326.     the repeat counter...
  327.     This is exactly what XBIN compression does...
  328.  
  329.     The XBIN compression consists out of a sequence of repeat counters
  330.     followed by the appropriate number of data bytes.
  331.  
  332.     Ok, before we go any further now, allow me to make an important note.
  333.     XBIN compression works on a ROW by ROW basis.  The compression does NOT
  334.     carry through to the next line.  so if you would have for example two
  335.     lines like this :
  336.                    Ab,Aa,Aa,Aa
  337.                    Aa,Aa,Aa,Ab
  338.     It should NOT be encoded as
  339.                    Ab,[Repeat 6 Times]Aa,Ab
  340.     but SHOULD be encoded as
  341.                    Ab,[Repeat 3 Times]Aa
  342.                    [Repeat 3 Times]Aa,Ab
  343.     The are several reasons why it should work like this
  344.        1) on-the-fly decoding is facilitated (more on this later)
  345.        2) You could run into a problem with odd XBIN widths since video
  346.           memory memory always has an even width.  You'd have to implement
  347.           special code to skip over this one odd byte.
  348.        3) You at least have SOME way of detecting errors in the XBIN file.
  349.           Whenever a line doesn't nicely work out to the required width
  350.           there must be some error in the XBIN file (or a bug in your
  351.           decoding routine ;-))
  352.  
  353.  
  354.     XBIN Compression continued
  355.     ---------------------------
  356.     The repeat counter is split up in two parts, the two most significant
  357.     bits are the compression type, the six least significant bits are the
  358.     actual repeat counter.
  359.  
  360.     [ X X . . . . . . ]  Compression type
  361.                          00 : No compression
  362.                          01 : Character compression
  363.                          10 : Attribute compression
  364.                          11 : Character/Attribute compression.
  365.     [ . . X X X X X X ]  Repeat counter.
  366.                          Since a repeat of 0 is downright useless, we'll
  367.                          store the repeat counter as one less of it's actual
  368.                          number of repeats.
  369.                          A value of 0 indicates a repeat value of 1.
  370.                          A value of 1 indicates a repeat value of 2.
  371.                          And so on, giving us a maximum
  372.     In the examples, the Repeat counter byte will be used as [Type,Count]
  373.     Where Type is 00, 01, 10 or 11, and Count is a number from 0 to 63.
  374.     For example, [01,10] would mean, Attribute compression with a repeat
  375.     counter of 10 (11 effective repeats)
  376.  
  377.    * Compression type 00 : No compression
  378.       This type of compression is needed whenever two or more sequences of
  379.       character/attribute pairs have nothing in common.
  380.       The repeat counter is followed by the appropriate number of
  381.       character/attribute pairs.
  382.  
  383.       Data            : AaBbCcDdEeFfGg
  384.       XBin Compressed : [00,6]AaBbCcDdEeFfGg
  385.                             └── Remember value is ONE less than the actual
  386.                                 number of repeats wanted.
  387.  
  388.       In a worst-case situation where you would have to use No compression on
  389.       the entire file, this would mean the 'compressed' data is bigger as the
  390.       non-compressed data, in this situation, the best thing to do would be
  391.       to store the image data uncompressed, and set the 'Compress' bit in the
  392.       Flags field to 0.
  393.  
  394.    * Compression type 01 : Character compression
  395.       This type of compression is used whenever a sequence of identical
  396.       characters is found, but where the attribute changes.
  397.       The repeat counter is followed by the character to use which is in turn
  398.       followed by the appropriate number of attribute bytes.
  399.  
  400.       Data            : AaAbAcAdAeAfAg
  401.       XBIN Compressed : [01,6]Aabcdefg
  402.  
  403.    * Compression type 10 : Attribute compression
  404.       This type of compression is used whenever a sequence of identical
  405.       attributes is found, but where the character changes.
  406.       The repeat counter is followed by the attribute to use which is in turn
  407.       followed by the appropriate number of character bytes.
  408.  
  409.  
  410.       Data            : AaBaCaDaEaFaGa
  411.       XBIN Compressed : [10,6]aABCDEFG
  412.  
  413.       ! Note this is the only time you'll see the attribute byte BEFORE the
  414.         characters.
  415.  
  416.    * Compression type 11 : Character/Attribute compression
  417.       This type of compression is used whenever a sequence of identical
  418.       character attribute pairs is found.
  419.       The repeat counter is followed by the character attribute pair.
  420.  
  421.       Data            : AaAaAaAaAaAaAa
  422.       XBIN Compressed : [11,6]Aa
  423.  
  424.     You'll find example code on how to do XBIN compression and decompression
  425.     lateron in the XBIN.TUT document.
  426.  
  427.  
  428.   XBIN freebies
  429.   --------------
  430.   Alongside with this document you should find the following files :
  431.  
  432.   XBIN.TUT       Small tutorial on how to program the VGA in text mode.
  433.                  Setting, palette, entering 512 character mode, setting fonts
  434.                  Setting font sizes, (de)compressing XBIN and on-the-fly
  435.                  decompressing of XBIN.
  436.   SIMPLEXB.EXE   Simple-XB, a simple yet sturdy XBIN viewer.  Care has been
  437.                  taken so it supports ANY valid XBIN you throw at it, it may
  438.                  behave erratic for invalid XBIN files though.
  439.                  it's not the fastest XBIN viewing possible, but it DOES
  440.                  support ALL possible XBIN files, even 8Gb ones <g>.
  441.                  You can use SIMPLEXB as a basis for your own viewer and use
  442.                  it as a test case to test validity of XBIN files.
  443.   SIMPLEXB.PAS   Sourcecode to Simple-XB
  444.                  It's main intention is to serve as a learning platform
  445.                  on how to use XBIN files, alongside of being a testing
  446.                  platform for XBIN conpliance.
  447.   BIN2XBIN.EXE   Conversion program to convert BIN files to XBIN.  It is
  448.                  provided in executable form for those who do not have Turbo
  449.                  Pascal.
  450.   BIN2XBIN.PAS   Sourcecode to BIN2XBIN
  451.                  BIN2XBIN serves as a learning platform on how one COULD
  452.                  implement an XBIN compressor.  BIN2XBIN combines several
  453.                  BIN files (up to 10 by 10 screens) into a single XBIN.
  454.                  No palette or font information is stored.
  455.   ADF2XBIN.EXE   Similar to BIN2XBIN, but for converting a single ADF file to
  456.                  XBIN
  457.   ADF2XBIN.PAS   Sourcecode for ADF2XBIN
  458.   STM.PAS        Unit used by both SIMPLEXB.PAS and BIN2XBIN.PAS
  459.                  the STM unit provides buffered file input & output to files
  460.                  providing a significant speed increase over using a file of
  461.                  bytes, or even blockwrite/blockread on relatively small
  462.                  chunks of data.
  463.   VGA.PAS        Unit used by SIMPLEXB.PAS
  464.                  The VGA unit provides the low level VGA specific routines.
  465.   CT-XBIN.XB
  466.   T1-XBIN.XB
  467.   US-XBIN.XB     Several XBIN files so you can have a look at what it can do
  468.                  And so that you can test if your own viewer works correctly.
  469.  
  470.                                  -THE-END-
  471. SAUCE00eXtended BIN format specification  Tasmaniac           ACiD Productions    19960801(RP╓